const app = getApp();
Page({
  data: {
    inputShowed: false,
    inputVal: "",
    searchItem:'',
    contacts:[]
  },
  onShow:function(){
    this.getContacts();
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
    this.getContacts();
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
    this.getContacts();
  },
  // 最近转账
  getContacts() {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetRecentContacts',
      data: {
        type: 2,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            contacts: res.data.result.items
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 查询
  inputTyping(e){
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetRecentContacts',
      data: {
        type: 2,
        keyWord: e.detail.value
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            contacts: res.data.result.items
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 点击事件
  getContact(e) {
    var idx = e.currentTarget.id;
    console.log(idx);
    this.setData({
      searchItem: this.data.contacts[idx].phoneNumber
    })
    wx.setStorage({
      key: "contact",
      data: this.data.searchItem
    })
    wx.navigateBack();
  }
});